// pages/product/product.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        share:{
            flag:false
        },
        show:{
            isShow:false,
            type:'',
            service:''
        },
        product:{
            id:'',
            name:'追源酸枣糕',
            collection:false,
            picUrl:[['https://vkceyugu.cdn.bspapp.com/VKCEYUGU-b1ebbd3c-ca49-405b-957b-effe60782276/c8c4e05e-42d4-4a22-b111-85765d4e22e2.jpg']],
            price:[[1,2,3],[4,5,6],[7,8,9],[10,11,12]],
            stock:50000,
            selected_size:0,
            selected_flavor:0,
            size:['220克/罐','礼盒装(4罐)','100克/袋','100克/4袋(包邮)'],
            flavor:['酸甜口味','偏甜','偏酸'],
            num:1,
            swiperPic:[
                {
                    id:0,
                    picUrl:'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-b1ebbd3c-ca49-405b-957b-effe60782276/c8c4e05e-42d4-4a22-b111-85765d4e22e2.jpg'
                },
                {
                    id:1,
                    picUrl:'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-b1ebbd3c-ca49-405b-957b-effe60782276/fe5343f9-b925-4759-a423-e0005f965ede.jpg'
                },
                {
                    id:2,
                    picUrl:'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-b1ebbd3c-ca49-405b-957b-effe60782276/bae5ea1a-42ff-4fe1-a2bd-c88cecbff976.jpg'
                }
            ],
            packing:'罐装',
            technology:'果脯类',
            origin:'中国大陆'
        },
        goodsList:[],
        commentList:[
            {
                user_img:'/images/zhuiyuan_img.png',
                user_name:'夏莫',
                comment:'太好吃了！',
                time:'2021-2-19'
            }, 
            {
                user_img:'/images/zhuiyuan_img.png',
                user_name:'夏莫',
                comment:'太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！太好吃了！',
                time:'2021-2-19'
            }
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let userInfo = wx.getStorageSync('userInfo');
        if (!userInfo){
            wx.showModal({
                title:'提示',
                content:'您还没有登录',
                success(res){
                    if (res.confirm){
                        // wx.switchTab({
                        //   url: '/pages/ucenter/index/index',
                        // })
                        app.getuserInfo();
                    } else if (res.cancel){
                        wx.switchTab({
                            url: '/pages/index/index',
                        })
                    }
                }
            })
        }
        let shareid = options.shareid;

        if (shareid){
            wx.setStorageSync('shareid', shareid);
        }
        let goodslist = wx.getStorageSync('goodsList');
        this.setData({
            goodsList:goodslist
        })
        console.log(goodslist);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title:'sharetest',
            path:'/pages/product/product?shareid=' + '54185418'
        }
    },
    /**
     * (弹出层关闭监听事件)
     * @徐志鹏
     * @2021-2-2
     */
    onClose:function(){
        let show = this.data.show;
        
        show.isShow = false;
        this.setData({
            show:show
        })
    },
    /**
     * (弹出层弹出监听事件)
     * @徐志鹏
     * @2021-2-2
     */
    show_Popup_top:function(){
        let show = this.data.show;

        show.isShow = true;
        show.type = 'body';
        this.setData({
            show:show
        })
    },
    /**
     * (底部通过加入购物车打开弹出层监听事件)
     * @徐志鹏
     * @2021-2-2
     */
    show_Popup_button_cart:function(){
        let show = this.data.show;
        show.isShow = true;
        show.type = 'footer';
        show.service = 'cart';
        this.setData({
            show:show
        })
    },
     /**
     * (底部通过购买打开弹出层监听事件)
     * @徐志鹏
     * @2021-2-2
     */
    show_Popup_button_buy:function(){
        let show = this.data.show;

        show.isShow = true;
        show.type = 'footer';
        show.service = 'buy';
        this.setData({
            show:show
        })
    },
    /**
     * (选择商品规格事件)
     * @param {*} e 
     * @徐志鹏
     * @2021-2-1
     */
    change_size:function(e){
        let index = e.currentTarget.dataset.index;
        let product = this.data.product;

        product.selected_size = index;

        this.setData({
            product:product
        })
    },
    /**
     * (选择商品口味事件)
     * @param {*} e 
     * @徐志鹏
     * @2021-2-1
     */
    change_flavor:function(e){
        let index = e.currentTarget.dataset.index;
        let product = this.data.product;

        product.selected_flavor = index;

        this.setData({
            product:product
        })
    },
    /**
     * (减少商品数量)
     * @徐志鹏
     * @2021-2-1
     */
    c_subNum:function(){
        let product = this.data.product;

        if (product.num === 1){
            wx.showToast({
                title: '不能再减了!',
                icon:'none'
            });
        }else {
            product.num--;
        }

        this.setData({
            product:product
        })
    },
      /**
     * (增加商品数量)
     * @徐志鹏
     * @2021-2-1
     */
    c_addNum:function(){
        let product = this.data.product;

        product.num++;

        this.setData({
            product:product
        })
    },
     /**
     * (确定按钮监听事件)
     * @徐志鹏
     * @2021-2-1
     */
    determine:function(){
        let show = this.data.show;
        let pro = this.data.product;
        let product = {};

        if (show.service === 'buy'){
            product.id = pro.id;
            product.name = pro.name;
            product.picUrl = pro.picUrl[0][0];
            product.price = pro.price[pro.selected_size][pro.selected_flavor];
            product.size = pro.size[pro.selected_size];
            product.flavor = pro.flavor[pro.selected_flavor];
            product.num = pro.num;
            console.log(product);
            wx.navigateTo({
              url: '/pages/order/order?product=' + JSON.stringify(product),
            })
        }else if (show.service === 'cart'){
            wx.showToast({
              title: '加入购物车成功！',
            })
            console.log(this.data.product);
        }
    },
     /**
     * (body弹出层加入购物车事件)
     * @徐志鹏
     * @2021-2-1
     */
    body_cart:function(){
        let show = this.data.show;

        show.service = 'cart';
        console.log(show); 
    },
    /**
     * (body弹出层购买事件)
     * @徐志鹏
     * @2021-2-1
     */
    body_buy:function(){
        let show = this.data.show;
        let pro = this.data.product;
        let product = {};

        product.id = pro.id;
        product.name = pro.name;
        product.picUrl = pro.picUrl;
        product.price = pro.price[pro.selected_size][pro.selected_flavor];
        product.size = pro.size[pro.selected_size];
        product.flavor = pro.flavor[pro.selected_flavor];
        product.num = pro.num;

        show.service = 'buy';
        wx.navigateTo({
          url: '/pages/order/order?product=' + JSON.stringify(product),
        })
    },
    /**
     * (跳转评论页面)
     * @param {*} e 
     * @徐志鹏
     * @2021-2-19
     */
    comment:function(){
        let commentlist = this.data.commentList;
        wx.setStorageSync('commentList', commentlist)
        wx.navigateTo({
          url: '/pages/comment/comment?commentList='+commentlist,
        })
    },
    /**
     * (添加收藏)
     * @param {*} e
     * @徐志鹏
     * @2021-2-22 
     */
    collection:function(e){
        let product = this.data.product;
        
        if (product.collection){
            wx.showToast({
              title: '已经在收藏里了！',
              icon:'none'
            })
        } else {
            product.collection = true;
            this.setData({
                product:product
            })
            wx.showToast({
              title: '添加成功！',
            })
        }
    },
    share:function(e){
        let share = this.data.share;
        if (share.flag){
            share.flag = false;
        }else {
            share.flag = true;
        }

        this.setData({
            share:share
        })
    },
    /**
     * @(图片保存到本地事件)
     * @author 徐志鹏
     * @date 2021-3-15
     */
    saveImage:function(){
        wx.showModal({
            title: '提示',
            content: '是否保存在系统相册',
            success (res) {
                if (res.confirm) {
                    wx.showLoading({
                      title: '请稍后',
                    })
                    wx.downloadFile({
                        url: 'http://f0.0sm.com/node0/2021/03/8604F6C70C0A51C4-44bb3bb54af8e815.jpg',
                        success:function(res){
                            console.log(res);
                            wx.saveImageToPhotosAlbum({
                              filePath: res.tempFilePath,
                              success:function(){
                                wx.hideLoading({
                                  success: (res) => {},
                                })  
                                wx.showToast({
                                    title: '保存成功！',
                                    icon:'none'
                                })
                              },
                              fail:function(){
                                  wx.showToast({
                                    title: '保存失败，尝试长按图片保存',
                                    icon:'none'
                                  })
                              }
                            })
                        }
                    })
                } else if (res.cancel) {
                    return;
                }
            }
        })
        
        
    }
})